package com.starsea.book.ui.newpage.utils

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.bytedance.sdk.openadsdk.AdSlot
import com.bytedance.sdk.openadsdk.TTAdConstant
import com.bytedance.sdk.openadsdk.TTAdLoadType
import com.bytedance.sdk.openadsdk.TTAdNative
import com.bytedance.sdk.openadsdk.TTAdSdk
import com.bytedance.sdk.openadsdk.TTRewardVideoAd
import com.google.gson.Gson
import com.google.gson.JsonParser
import com.starsea.book.R
import com.starsea.book.ui.newpage.entity.BaseEntity
import com.starsea.book.utils.toastOnUi

/**
 * @author Lee
 * @createTime 2023-12-07 17 GMT+8
 * @desc :
 */

fun <T> String.gsonList(cls: Class<T>): BaseEntity<MutableList<T>> {

    val baseEntity = BaseEntity<MutableList<T>>()

    baseEntity.data = arrayListOf()

    val gson = Gson()
    val objects = JsonParser.parseString(this).asJsonObject
    baseEntity.status = objects.get("status").asInt
    baseEntity.msg = objects.get("msg").asString
    if (baseEntity.status != 200) {
        return baseEntity
    }
    val arry = objects.getAsJsonArray("data")
    arry.forEach {
        baseEntity.data!!.add(gson.fromJson(it, cls))
    }
    return baseEntity
//    val type = object : TypeToken<BaseEntity<List<T>>>() {}.type
//    val entity: BaseEntity<List<T>> = Gson().fromJson(this, type)
//    return entity
}

fun <T> String.gsonNormal(cls: Class<T>): BaseEntity<T> {

    val baseEntity = BaseEntity<T>()
    val gson = Gson()
    val objects = JsonParser.parseString(this).asJsonObject
    baseEntity.status = objects.get("status").asInt
    baseEntity.msg = objects.get("msg").asString

    if (baseEntity.status != 200) {

        return baseEntity
    }

    val data = objects.get("data")
//    val type = object : TypeToken<T>() {}.type
//    baseEntity.data = Gson().fromJson(gson.fromJson(data), cls)
    baseEntity.data = gson.fromJson(data, cls)

    return baseEntity
}

fun <T> String.gsonNormalNull(cls: Class<T>): BaseEntity<T?> {

    val baseEntity = BaseEntity<T?>()
    val gson = Gson()
    val objects = JsonParser.parseString(this).asJsonObject
    baseEntity.status = objects.get("status").asInt
    baseEntity.msg = objects.get("msg").asString

    if (baseEntity.status != 200) {

        return baseEntity
    }

    val data = objects.get("data")
//    val type = object : TypeToken<T>() {}.type
//    baseEntity.data = Gson().fromJson(gson.fromJson(data), cls)
    baseEntity.data = gson.fromJson(data, cls)

    return baseEntity
}

fun ImageView.loadRoundImage(mContext: Context, url: String?) {

    Glide.with(mContext)
        .load(url)
        .apply(RequestOptions.circleCropTransform())
        .placeholder(R.mipmap.cicrle_default)
        .into(this)
}

fun ImageView.loadNormalImage(mContext: Context, url: String) {
    Glide.with(mContext)
        .load(url)
        .into(this)
}


/**
 * 设置view显示
 */
fun View.visible() {
    visibility = View.VISIBLE
}

/**
 * 设置view占位隐藏
 */
fun View.invisible() {
    visibility = View.INVISIBLE
}

/**
 * 根据条件设置view显示隐藏 为true 显示，为false 隐藏
 */
fun View.visibleOrGone(flag:Boolean) {
    visibility = if(flag){
        View.VISIBLE
    }else{
        View.GONE
    }
}

/**
 * 根据条件设置view显示隐藏 为true 显示，为false 隐藏
 */
fun View.visibleOrInvisible(flag:Boolean) {
    visibility = if(flag){
        View.VISIBLE
    }else{
        View.INVISIBLE
    }
}

/**
 * 设置view隐藏
 */
fun View.gone() {
    visibility = View.GONE
}


fun Activity.loadAdVideo() {
    val mTTAdNative =
        TTAdSdk.getAdManager().createAdNative(this)

    val adSlot = AdSlot.Builder()
        .setCodeId("956204408")
        .setOrientation(TTAdConstant.VERTICAL)
        .setAdLoadType(TTAdLoadType.LOAD)
        .build()

    val mContext = this

    mTTAdNative.loadRewardVideoAd(adSlot,
        object : TTAdNative.RewardVideoAdListener {
            override fun onError(p0: Int, p1: String?) {
                com.blankj.utilcode.util.LogUtils.e(">>>> p0 = ${p0} \n p1 = ${p1}")
            }

            override fun onRewardVideoAdLoad(p0: TTRewardVideoAd?) {
            }

            override fun onRewardVideoCached() {
            }

            override fun onRewardVideoCached(ttRewardVideoAd: TTRewardVideoAd?) {

                ttRewardVideoAd!!.showRewardVideoAd(mContext)

                ttRewardVideoAd.setRewardAdInteractionListener(object :
                    TTRewardVideoAd.RewardAdInteractionListener {
                    override fun onAdShow() {
                        com.blankj.utilcode.util.LogUtils.e(">>>> 广告展示")
                        // 广告展示
                    }

                    override fun onAdVideoBarClick() {
                        // 广告点击
                        com.blankj.utilcode.util.LogUtils.e(">>>> 广告点击")
                    }

                    override fun onAdClose() {
                        // 广告关闭
                        com.blankj.utilcode.util.LogUtils.e(">>>> 广告关闭")
                    }

                    override fun onVideoComplete() {
                        com.blankj.utilcode.util.LogUtils.e(">>>> 广告素材播放完成，例如视频未跳过，完整的播放了")
                    }

                    override fun onVideoError() {
                        com.blankj.utilcode.util.LogUtils.e(">>>> 广告展示时出错")
                    }

                    override fun onRewardVerify(
                        p0: Boolean,
                        p1: Int,
                        p2: String?,
                        p3: Int,
                        p4: String?
                    ) {
                    }

                    // 奖励发放
                    override fun onRewardArrived(
                        p0: Boolean,
                        p1: Int,
                        p2: Bundle?
                    ) {
                        toastOnUi("奖励已发放")

//                        viewModel.adverReward(data, type)

                        com.blankj.utilcode.util.LogUtils.e(">>>> 奖励发放")
                        com.blankj.utilcode.util.LogUtils.e(">>>> p0 = ${p0} \n p1 = ${p1} \n ")
                    }

                    // // 用户在观看时点击了跳过
                    override fun onSkippedVideo() {
                        com.blankj.utilcode.util.LogUtils.e(">>>> 用户在观看时点击了跳过")
                    }

                })
            }
        })


}
